package com.cgsd.heyu.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.cgsd.heyu.R;

/**
 * @author xiaoxing
 * Description 确认对话框
 * CreateData: 2020/11/3
 */
public class ConfirmDialog extends Dialog implements View.OnClickListener {
    private Context mContext;
    private TextView tvTitle;
    private TextView tvCancel,tvConfirm;
    private DialogListener dialogListener;

    public void setDialogListener(DialogListener dialogListener) {
        this.dialogListener = dialogListener;
    }

    public ConfirmDialog(@NonNull Context context) {
        super(context, R.style.dialog_custom);
        this.mContext = context;
        setContentView(R.layout.dialog_confrim);
        Window window = this.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
        }
        setCanceledOnTouchOutside(false);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        // 获取屏幕宽、高用
        DisplayMetrics d = mContext.getResources().getDisplayMetrics();
        // 宽度设置为屏幕的0.7
        lp.width = (int) (d.widthPixels * 0.8);
        dialogWindow.setAttributes(lp);
        initView();
    }

    private void initView() {
        tvTitle = findViewById(R.id.tv_title);
        tvCancel = findViewById(R.id.tv_cancel);
        tvConfirm = findViewById(R.id.tv_confirm);
        tvConfirm.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_cancel:
                if (dialogListener != null){
                    dialogListener.cancel();
                }
                break;
            case R.id.tv_confirm:
                if (dialogListener != null){
                    dialogListener.confirm();
                }
                break;
            default:
        }
    }

    /**
     * 设置按钮文案
     */
    public void setTvButton(String cancel,String confirm){
        tvCancel.setText(cancel);
        tvConfirm.setText(confirm);
    }

    /**
     * 设置标题
     */
    public void setTitle(String title){
        tvTitle.setText(title);
    }


    public interface DialogListener{
        /**
         * 确认
         */
        void confirm();

        /**
         * 取消
         */
        void cancel();
    }
}
